<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Continent extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "continent";
		$this->prefix = "con";
	}

	function getContinentList($all = FALSE) {
		$qs =	"
					select
						con_id, con_name, con_url_name, cou_id, cou_name, cou_url_name, ins_id, city_id, city_name, city_url_name, ins_id, ins_name, ins_url_name, maj_id, maj_name, maj_url_name
					from
						".$this->db->dbprefix.$this->tableName."
						left join ".$this->db->dbprefix."country on con_id = cou_con_id and cou_del = 0 ".(($all)?(" and cou_presenter = 'I' "):(""))."
						left join ".$this->db->dbprefix."city on cou_id = city_cou_id and city_del = 0 ".(($all)?(" and city_presenter = 'I' "):(""))."
						left join ".$this->db->dbprefix."institute on city_id = ins_city_id  and ins_del = 0 ".(($all)?(" and ins_presenter = 'I' "):(""))."
						left join ".$this->db->dbprefix."major on ins_id = maj_ins_id  and maj_del = 0 ".(($all)?(" and maj_presenter = 'I' "):(""))."
					where
						con_del = 0
					order by
						con_name, cou_name, city_name, ins_name, maj_name
					";
		$result = $this->db->query($qs);
		$getContinentList = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getContinentList[$row->con_id]["conName"] = $row->con_name;
				$getContinentList[$row->con_id]["url"] = site_url().$row->con_url_name;
				if(($row->cou_id and ($all or (Common::isSuperAdmin() or (Common::isAdmin() and ($row->u2e_cou_id or $row->u2e_city_id or $row->u2e_ins_id)))))) {
					$getContinentList[$row->con_id]["cou"][$row->cou_id]["couName"] = $row->cou_name;
					$getContinentList[$row->con_id]["cou"][$row->cou_id]["url"] = site_url().$row->con_url_name."/".$row->cou_url_name;
					if(($row->city_id and ($all or (Common::isSuperAdmin() or (Common::isAdmin() and ($row->u2e_city_id or $row->u2e_ins_id)))))) {
						$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["cityName"] = $row->city_name;
						$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["url"] = site_url().$row->con_url_name."/".$row->cou_url_name."/".$row->city_url_name;
						if(($row->ins_id and ($all or (Common::isSuperAdmin() or (Common::isAdmin() and $row->u2e_ins_id))))) {
							$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["ins"][$row->ins_id]["insName"] = $row->ins_name;
							$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["ins"][$row->ins_id]["url"] = site_url()."institutes/".$row->ins_url_name;
							if($row->maj_id) {
								$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["ins"][$row->ins_id]["maj"][$row->maj_id]["majName"] = $row->maj_name;
								$getContinentList[$row->con_id]["cou"][$row->cou_id]["city"][$row->city_id]["ins"][$row->ins_id]["maj"][$row->maj_id]["url"] = site_url()."institutes/".$row->ins_url_name."/".$row->maj_url_name;
							}
						}
					}
				}
			}
		}

		return $getContinentList;
	}

	function getContinent($where = NULL, $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "conName", $dir = "ASC") {

		$fieldsQuery = Common::fieldsquery($fields, $query);
		$order = Common::coalesce(array($order, "conName"));
		$dir = Common::coalesce(array($dir, "ASC"));

		$qs =	"
					select
						con_id, con_name, con_url_name, con_latitude, con_longitude, t.e2c_temp_flag
					from
						".$this->db->dbprefix.$this->tableName."
						left join (
							select
								e2c_ent_id, e2c_temp_flag
							from
								".$this->db->dbprefix."ent2cat 
							where
								e2c_entity = 'CON' and e2c_del = 0 and e2c_temp_flag = 'I'
							group by
								e2c_ent_id
						) as t on t.e2c_ent_id = con_id
					where
						con_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					order by
						".(($order == "rand()")?($order):(Common::underscore($order)." ".$dir))."
					".((is_numeric($start))?(" limit ".$start.",".$limit):(""))."
					";
		$result = $this->db->query($qs);

		$getContinent = $couIds = $catIds = $e2c = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				$getContinent[] = array(
					"conId" => $row->con_id,
					"conName" => $row->con_name,
					"conUrlName" => $row->con_url_name,
					"conLatitude" => $row->con_latitude,
					"conLongitude" => $row->con_longitude,
					"e2cTempFlag" => $row->e2c_temp_flag,
				);
			}

		}

		return $getContinent;
	}

	function getContinentCount($where = NULL, $fields, $query) {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						count(*) as cnt
					from
						".$this->db->dbprefix.$this->tableName."
					where
						con_del = 0 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					";
		$result = $this->db->query($qs);

		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return $row->cnt;
			}
		}
	}

}